package com.wxlsoft.excel.convert.handlers;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wxlsoft.excel.convert.model.ServiceBody;
import com.wxlsoft.excel.convert.model.ServicePackage;

import cn.hutool.core.util.StrUtil;

/**
 * 报文数据转换
 * @author wangxiaoliang
 */
public class ServicePackageConverter {

	public final static Pattern COMPILE = Pattern.compile("(.*)\\((.*)\\)$");
	
	/**
	 * 公共报文转换
	 * @param common
	 * @return
	 */
	public static void convertCommonPackage(ServicePackage common) {
		common.getInputs().stream().forEach(i -> i.setEnNamer(StrUtil.toCamelCase(i.getEnNamer())));
		common.getOutputs().stream().forEach(o -> o.setEnNamer(StrUtil.toCamelCase(o.getEnNamer())));
	}

	/**
	 * 报文映射转换
	 * @param service
	 */
	public static void convertServicePackage(ServicePackage service) {
		Boolean isStandard = service.getIsStandard();
		
		mapping(service.getInputs(), isStandard);
		mapping(service.getOutputs(), isStandard);
	}

	private static void mapping(List<ServiceBody> bodys, Boolean isStandard) {
		for (ServiceBody body : bodys) {
			// 如果是标准报文，右侧直接将左侧覆盖
			if (isStandard) {
				body.setEnNamel(body.getEnNamer());
				body.setCnNamel(body.getCnNamer());
				Matcher matcher = COMPILE.matcher(body.getDataTyper());
				if (matcher.matches()) {
					body.setDataTypel(matcher.group(1));
					body.setDataLengthl(matcher.group(2));
				}
				body.setIsNeedInputl(body.getIsNeedInputr());
				body.setRemarkl(body.getRemarkr());
			}
			body.setEnNamer(StrUtil.toCamelCase(body.getEnNamer()));
		}
	}

}
